----------------------
-- Background class
----------------------
Background = {}
Background.__index = Background

function Background.new(image_url, bkgd_type, x, y, vel_x, vel_y, repeat_x, repeat_y, container)
	local b = {}
	setmetatable(b, Background)
	
	-- variables
	b.c_type = "background"
	-- if scroll vel   = -infi -> infi
 	-- if parallax vel = 0.0   -> 1.0
	b.x = x
	b.y = y
	b.vel_x = vel_x
	b.vel_y = vel_y
	b.bkgd_type = bkgd_type -- "still", "scroll", "parallax"
	b.sprite = Sprite.new()
	b.sprite:loadImage("default", image_url)
    b.repeat_x = repeat_x
    b.repeat_y = repeat_y
	
	-- defaults
	if b.bkgd_type == nil then b.bkgd_type = "still" end
	if b.x == nil then b.x = 0.0 end
	if b.y == nil then b.y = 0.0 end
	if b.vel_x == nil then b.vel_x = 0.0 end
	if b.vel_y == nil then b.vel_y = 0.0 end
    if b.container == nil then b.container = 0 end
    
    b.current_container = 0
	
	return b
end

----------------------
-- Update
-- Camera, x_vel_offset and y_vel_offset
-- you can fix the speed of scolling background when
-- obj is moving.
----------------------
function Background:update(camera, x_vel_offset, y_vel_offset, container_id)
    -- set container id
    self.current_container = container_id
	if self.bkgd_type == "scroll" then
        self.x = self.x + self.vel_x
        self.y = self.y + self.vel_y
    
--         if camera ~= nil then
--             if  camera.x > camera.clamp_x and 
--                 camera.x < (camera.clamp_width - global_width) and 
--                 self.vel_x ~= 0 then
--                 
--                 --self.x = self.x - x_vel_offset
--                 if x_vel_offset < 0 then
--                     self.x = self.x + self.vel_x
--                 else
--                     self.x = self.x + self.vel_x * 0.5
--                 end
--             end
--             
--             if  camera.y > camera.clamp_y and 
--                 camera.y < (camera.clamp_height - global_height) and 
--                 self.vel_y ~= 0 then
--                 
--                 self.y = self.y - y_vel_offset
--             end
--         end
        
		self.x = self.x % (-global_width + (global_width - self.sprite:getWidth()))
		self.y = self.y % (-global_height + (global_height - self.sprite:getHeight()))
	end
end

----------------------
-- Draw
----------------------
function Background:draw(camera)
    -- do not draw if container is not 0 (all) and
    -- current container is not container
    if self.container ~= 0 then
        if self.container ~= self.current_container then
            return
        end
    end
    
	local sp_w = self.sprite:getWidth()
	local sp_h = self.sprite:getHeight()
	local count_x = math.ceil(global_width / sp_w)
	local count_y =  math.ceil(global_height / sp_h)
    
    if not self.repeat_x then count_x = 0 end
    if not self.repeat_y then count_y = 0 end

	if self.bkgd_type == "still" then
		for y = 0, count_y do
			for x = 0, count_x do
				self.sprite:draw((x * sp_w), (y * sp_h))
			end
		end
	end
	
	if self.bkgd_type == "scroll" then
		for y = 0, count_y do
			for x = 0, count_x do
				self.sprite:draw(self.x + (x * sp_w), self.y + (y * sp_h))
			end
		end
	end
	
	if self.bkgd_type == "parallax" then
		local x_dif = camera.x * self.vel_x
		local y_dif = camera.y * self.vel_y
		local x_offset = (camera.x - x_dif) % self.sprite:getWidth()
		local y_offset = (camera.y - y_dif) % self.sprite:getHeight()
		
		for y = 0, count_y do
			for x = 0, count_x do
                -- original
				-- self.sprite:draw(-x_offset + (x * sp_w), -y_offset + (y * sp_h))
                -- test fix this someday
                self.sprite:draw(-x_offset + (x * sp_w) + self.x, -y_offset + (y * sp_h) + self.y)
			end
		end
	end
end